package com.yanqu.road.server.manager.activity.hsisland.handler.impl;

import com.yanqu.road.entity.activity.hsisland.HsIslandExploreResult;
import com.yanqu.road.entity.activity.hsisland.config.HsIslandConfig;
import com.yanqu.road.entity.activity.hsisland.data.HsIslandEventData;
import com.yanqu.road.server.manager.activity.hsisland.CrossHsIslandActivity;
import com.yanqu.road.server.manager.activity.hsisland.CrossHsIslandIsland;
import com.yanqu.road.server.manager.activity.hsisland.CrossHsIslandUnion;
import com.yanqu.road.server.manager.activity.hsisland.CrossHsIslandUser;
import com.yanqu.road.server.manager.activity.hsisland.handler.param.HsIslandExploreHandlerReq;
import com.yanqu.road.server.manager.activity.hsisland.handler.param.HsIslandExploreHandlerResp;
import com.yanqu.road.server.manager.activity.hsisland.handler.HsIslandIEventHandler;
import com.yanqu.road.utils.RandomHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class HsIslandEventPvpHandler implements HsIslandIEventHandler {

    /**
     * 探索后处理
     * @param req
     * @return
     */
    @Override
    public HsIslandExploreResult explore(HsIslandExploreHandlerReq req) {
        CrossHsIslandActivity activity = req.getActivity();
        HsIslandEventData eventData = req.getEventData();
        CrossHsIslandUser user = req.getUser();
        CrossHsIslandIsland island = req.getIsland();
        //事件注入坐标
        eventData.addPosition(req.getX(), req.getY());
        //随机出一个商会
        CrossHsIslandUnion pvpUnion = activity.getRandomPvpUnion(user.getUserData().getUnionId());
        //随机出5个玩家
        Set<Long> pvpUserSet = RandomHelper.selectRandomElements(pvpUnion.getUserMap().keySet(), 5);
        int pvpUserCount = pvpUserSet.size();
        //打乱顺序
        List<Long> pvpUserIdList = new ArrayList<>();
        for (int i = 0; i < pvpUserCount; i++) {
            Long pvpUserId = RandomHelper.getRandomElement(pvpUserSet);
            if (pvpUserId != null) {
                pvpUserIdList.add(pvpUserId);
                pvpUserSet.remove(pvpUserId);
            }
        }
        //事件注入pvp商会信息
        eventData.setPvpUnionId(pvpUnion.getUnionData().getUnionId());
        eventData.setPvpUserIds(pvpUserIdList);
        //岛屿注入事件
        island.putEvent(eventData);
        //返回
        HsIslandExploreResult resp = new HsIslandExploreResult();
        resp.addEventData(eventData);
        return resp;
    }
}
